.PHONY: clear imx

NAME := epit
ALL := $(NAME).bin

CRSS_COMPILE := arm-linux-gnueabihf-

CC := $(CRSS_COMPILE)gcc
LD := $(CRSS_COMPILE)ld
OBJCOPY := $(CRSS_COMPILE)objcopy
OBJDUMP := $(CRSS_COMPILE)objdump

INCDIRS := imx6ul \
           project \
           bsp/clk \
           bsp/led \
           bsp/beep \
           bsp/key \
           bsp/gpio \
           bsp/delay \
           bsp/int \
           bsp/exti \
           bsp/epit

SRCDIRS := project \
           bsp/clk \
           bsp/led \
           bsp/beep \
           bsp/key \
           bsp/gpio \
           bsp/delay \
           bsp/int \
           bsp/exti \
           bsp/epit

CFILES := $(shell find $(SRCDIRS) -name '*.c')
SFILES := $(shell find $(SRCDIRS) -name '*.S')

INCLUDE := $(patsubst %,-I %, $(INCDIRS))

SFILENDIR := $(notdir $(SFILES))
CFILENDIR := $(notdir $(CFILES))

SOBJS := $(patsubst %, obj/%,$(SFILENDIR:%.S=%.o))
COBJS := $(patsubst %, obj/%,$(CFILENDIR:%.c=%.o))

OBJS := $(SOBJS) $(COBJS)

VPATH := $(SRCDIRS)

$(ALL): $(NAME)
	$(OBJCOPY) -O binary -S $^.elf $@
	$(OBJDUMP) -D -m arm $^.elf > $^.dis

$(NAME): $(OBJS)
	$(LD) -Timx6ul.lds $^ -o $@.elf

$(SOBJS): obj/%.o: %.S
	$(CC) -Wall -nostdlib -c -O2 $(INCLUDE) -o $@ $<

$(COBJS): obj/%.o: %.c
	$(CC) -Wall -nostdlib -c -O2 $(INCLUDE) -o $@ $<

imx: $(ALL)
	./imxdownload $^ ./imx 

clear:
	rm -rf $(OBJS) $(NAME).elf $(NAME).dis imx
